package com.example.mahao_final

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import androidx.fragment.app.Fragment
import com.example.mahao_final.UI.AddBlogFragment
import com.example.mahao_final.UI.BlogFragment
import com.example.mahao_final.UI.MyBolgFragment

class MainActivity : AppCompatActivity() {

    private lateinit var fabAddBlog: FloatingActionButton

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 获取 BottomNavigationView
        val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottomNavigationView)

        // 获取 FloatingActionButton
        fabAddBlog = findViewById(R.id.fab_add_blog)

        // 设置 FloatingActionButton 点击事件
        fabAddBlog.setOnClickListener {
            // 弹出添加博客的对话框
            showAddBlogDialog()
        }

        // 设置 BottomNavigationView 的选项选中监听器
        bottomNavigationView.setOnItemSelectedListener { item ->
            when (item.itemId) {
                R.id.BlogFragment -> {
                    // 如果选择了 BlogFragment 菜单项，显示 BlogFragment
                    replaceFragment(BlogFragment())
                    return@setOnItemSelectedListener true
                }
                R.id.MyBolgFragment -> {
                    // 如果选择了 MyBolgFragment 菜单项，显示 MyBolgFragment
                    replaceFragment(MyBolgFragment())
                    return@setOnItemSelectedListener true
                }
                else -> false
            }
        }

        // 如果是首次启动，加载默认的 Fragment（比如 BlogFragment）
        if (savedInstanceState == null) {
            bottomNavigationView.selectedItemId = R.id.BlogFragment // 设置默认选项
        }
    }

    // 用于替换 Fragment 的函数
    private fun replaceFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragmentContainerView, fragment)
            .commit()
    }

    // 弹出添加博客的对话框
    private fun showAddBlogDialog() {
        val addBlogFragment = AddBlogFragment()

        // 可选：传递数据到对话框 (比如用于填充输入框等)
        val bundle = Bundle()
        addBlogFragment.arguments = bundle

        // 显示对话框
        addBlogFragment.show(supportFragmentManager, "AddBlogDialog")
    }
}
